require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'datepicker'
], function($, common, ready, template, Collection, Model, datepicker) {

    var index = function() {
        this.init();
    };

    index.prototype = {
        init: function() {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'liguanjia/spend',
                condition: {
                    limit: 10,
                    offset: 0
                },
                callback: function() {
                    var prependString = '<li><a href="javascript:">订单金额合计: ' + this.result.order_money; + '</a></li>';
                    prependString += '<li><a href="javascript:">扣款金额合计: ' + this.result.money; + '</a></li>';
                    var pagination = $('#pagination .pagination').prepend(prependString);
                    $('#account').html(this.result.account);
                    $('#total_spend').html(this.result.total_spend);
                    $('#total_order_money').html(this.result.total_order_money);

                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;

            $('#start_at').on('click', function() {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd'
                });
            });

            $('#end_at').on('click', function() {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd'
                });
            });

            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.start_at = $('#start_at').val();
                _this.collection.condition.end_at = $('#end_at').val();
                _this.collection.condition.order_sn = $('#order_sn').val();
                _this.collection.reload();
            });
        }
    };

    new index();

});